#include "stdio.h"
#include "Dual_Link_List.h"
#include "hardware.h"

// create a link list with INT8 node by reading data from a given file;
// Return the neck node of created list if success to create it, or NULL will be returned.
p_ListNode create_link_list_with_INT8_Node(char* file_name) {
	FILE* pFile;
	pFile = fopen(file_name,"r");
	if(!pFile){
		printf("Fail to open file!\n");
		return NULL;
	}

	/*p_ListNode list = (p_ListNode)malloc(sizeof(ListNode));
	if(!list) {
		printf("The neck node is allocated failure!");
		return NULL;
	}
	list->next = list;
	list->prior = list;*/

	p_ListNode list = create_node(NULL);
	if(!list) {
		return NULL;
	}

	ASCII char_read_from_file = fgetc(pFile);
	while(char_read_from_file != EOF) {
		if(insert_node(list,get_length(list),(void*)atoi(&char_read_from_file)) == FAILURE) {
			printf("Fail to creat list.\n");
			return NULL;
		}
		char_read_from_file = fgetc(pFile);
	}

	return list;
}

// create a link list with ASCII node by reading data from a given file;
// Return the neck node of created list if success to create it, or NULL will be returned.
p_ListNode create_link_list_with_ASCII_Node(char* file_name) {
	FILE* pFile;
	pFile = fopen(file_name,"r");
	if(!pFile){
		printf("Fail to open file!\n");
		return NULL;
	}

/*	p_ListNode list = (p_ListNode)malloc(sizeof(ListNode));
	if(!list) {
		printf("The neck node is allocated failure!");
		return NULL;
	}
	list->next = list;
	list->prior = list;*/

	p_ListNode list = create_node(NULL);
	if(!list) {
		return NULL;
	}

	ASCII char_read_from_file = fgetc(pFile);
	while(char_read_from_file != EOF) {
		if(insert_node(list,get_length(list),(void*)(char_read_from_file) == FAILURE)) {
			printf("Fail to creat list.\n");
			return NULL;
		}
		char_read_from_file = fgetc(pFile);
	}

	return list;
}
